package com.itboye.textileexpres.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OrderDeltilteBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

/**
 * 
 * @author 交易关闭
 * 
 */
public class OrderDetailActivityShibai extends BaseActivity implements
		OnClickListener, Observer {
	TextView title;
	ImageView back, img_other;
	String type;
	MyOrderPresenter orderPresenter;
	OrderDeltilteBeans beans;
	String order_code;
	private TextView order_num;
	String addre;
	private TextView tvshouhuo, textPhone, textPadd, faburen, tvyunfei,
			tvprice;

	private LinearLayout litouytDetait;

	private String str;
	private TextView shengyutime,create_time;
	RelativeLayout mobile_rel;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_order_guanbi;
	}

	@Override
	public void onMyCreate() {
		title.setText(getString(R.string.h32));
		img_other.setVisibility(View.GONE);
		// TODO Auto-generated method stub

		order_code = getIntent().getStringExtra("order_code");
		System.out.println("order_code_code" + order_code);
		str = getIntent().getStringExtra("str");
		System.out.println("strstr" + str);
		beans = new OrderDeltilteBeans();
		type = getIntent().getStringExtra("type");
		// onSstartStr();
		orderPresenter = new MyOrderPresenter(this);
		orderPresenter.getorder(IsMobileNumPhone.isUid(), order_code);
		mobile_rel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ("".equals(IsMobileNumPhone.isUid())
						|| IsMobileNumPhone.isUid() == null) {
					Intent intent = new Intent(OrderDetailActivityShibai.this,
							LoginActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent(OrderDetailActivityShibai.this,
							ActivityCustomerService.class);
					startActivity(intent);
				}

			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == orderPresenter.Xiang_success) {
				beans = (OrderDeltilteBeans) entity.getData();
				System.out.println("notenote"
						+ beans.getItems().get(0).getName());
				getCounte(beans);
				onAddres(beans);
			} else if (entity.getEventType() == orderPresenter.Delete_success) {
				toast(entity.getData() + "");

			}
		}
	}

	
	private void onAddres(OrderDeltilteBeans beans) {
		if (beans.get_address() == null) {
			return;
		} else {
			addre = beans.get_address().getCountry()
					+ beans.get_address().getProvince()
					+ beans.get_address().getDetailinfo();
			tvshouhuo.setText(beans.getContactname());
			textPhone.setText(beans.get_address().getMobile());
			textPadd.setText(addre);

			
		}
	}

	private void getCounte(OrderDeltilteBeans beans) {
		System.out.println("LLLLLLLLLLLLLLLLL" + beans.getItems().size());
		for (int i = 0; i < beans.getItems().size(); i++) {

			if (beans.getContactname() == null) {
				toast("数据为空");
			} else {
				// onSstartStr();

				faburen.setText(getString(R.string.sender)+":" + beans.getPublisher_name());

				order_num.setText(order_code);
				create_time.setText(TimeFormat.DateFormat(beans.getItems().get(i).getCreatetime()));
				System.out.println("beansbeans" + beans.getItems().size());
				View view = LayoutInflater.from(OrderDetailActivityShibai.this)
						.inflate(R.layout.item_order_detail, null);
				TextView goods_name1_tv = (TextView) view
						.findViewById(R.id.goods_name1_tv);
				TextView goods_name2_tv = (TextView) view
						.findViewById(R.id.goods_name2_tv);
				TextView goods_detail_tv = (TextView) view
						.findViewById(R.id.goods_detail_tv);
				TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
				TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
				TextView num_tv = (TextView) view.findViewById(R.id.num_tv);

				goods_name1_tv.setText(beans.getPublisher_name());
				goods_name2_tv.setText(beans.getItems().get(i).getName());
				goods_detail_tv.setText(beans.getPublisher_name());
				color_tv.setText(beans.getItems().get(i).getSku_desc());
				price_tv.setText(PriceChange.Change(beans.getItems().get(i).getPrice()));
				num_tv.setText(beans.getItems().get(i).getCount());

				ImageView goods_img = (ImageView) view
						.findViewById(R.id.goods_img);
				
//				tvyunfei.setText(beans.getItems().get(i).getOri_price() + getString(R.string.element));
//				tvprice.setText(beans.getItems().get(i).getDt_origin_country());
				tvyunfei.setText(PriceChange.Change(beans.getPost_price()) + getString(R.string.element));
				tvprice.setText(PriceChange.Change(beans.getPrice())+getString(R.string.element));
				XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
						+ beans.getItems().get(i).getImg(), goods_img);
				MarginLayoutParams layoutParams = new MarginLayoutParams(
						MarginLayoutParams.WRAP_CONTENT,
						MarginLayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(5, 5, 5, 5);
				litouytDetait.addView(view, layoutParams);

			}
		}
	}

}
